package com.example.taximeter.controller;

import com.example.taximeter.bean.*;
import com.example.taximeter.service.TaximeterService;
import com.example.taximeter.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 计价器管理
 */
@RestController
@RequestMapping(value = "/taximeter", method = RequestMethod.POST)
public class TaximeterController {

    private TaximeterService taximeterService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);


    @Autowired
    public TaximeterController(TaximeterService taximeterService,UserService userService) {
        this.taximeterService = taximeterService;
        this.userService = userService;
    }

    //查看计价器列表
    @RequestMapping(value = "/all")
    public ReturnInfo findTaximeterAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                 @RequestBody TaximeterView taximeterView) {
        //添加日志
        userService.addUserLog(7,taximeterView.toString());
        return taximeterService.findTaximeterALL(Integer.valueOf(pageNum), Integer.valueOf(pageSize), taximeterView);
    }

    //更新计价器信息
    @RequestMapping(value = "/update")
    public ReturnInfo updateTaximeter(@RequestBody @Validated(GroupUpdate.class) TaximeterView  taximeterView) {
        String mes = taximeterService.updateTaximeter(taximeterView);
        if(mes.equals("成功")){
//            logger.info("更新计价器信息"+taximeterView.toString());
            //添加日志
            userService.addUserLog(5,taximeterView.toString());
        }
        return ReturnInfo.info1(mes);

    }
    //删除计价器信息
    @RequestMapping(value = "/delete/{id}")
    public ReturnInfo deleteTaximeter(@PathVariable int id){
        String mes = taximeterService.deleteTaximeter(id);
        if(mes.equals("成功")){
//            logger.info("更新计价器信息"+String.valueOf(id));
            //添加日志
            userService.addUserLog(6,String.valueOf(id));
        }
        return ReturnInfo.info1(mes);
    }

    //查看计价器列表（增加时）
    @RequestMapping(value = "/list")
    public List<TaximeterView> findTaximeterList(@RequestBody TaximeterView taximeterView) {
        return taximeterService.findTaximeterList(taximeterView);
    }

    //添加计价器（可批量）
    @RequestMapping(value = "/add")
    public ReturnInfo addTaximeter(@RequestBody @Validated(GroupCreate.class) Taximeter taximeter){
        String mes = taximeterService.addTaximeter(taximeter);
        if(mes.equals("成功")){
//            logger.info("添加计价器信息"+taximeter.toString());
            //添加日志
            userService.addUserLog(4,taximeter.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //删除计价器（可批量）
    @RequestMapping(value = "/deleteall")
    public ReturnInfo deleteTaximeterAll(@RequestBody TaximeterView taximeterView){
        String mes = taximeterService.deleteTaximeterall(taximeterView);
        if(mes.equals("成功")){
//            logger.info("删除计价器信息"+taximeterView.toString());
        }
        return ReturnInfo.info1(mes);
    }


}